<?php
namespace app\common;

/**
 * http请求类
 */
class HttpCurl {
    public static function GetRandStr($length)
    {
        $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $len = strlen($str) - 1;
        $randstr = '';
        for ($i = 0; $i < $length; $i++) {
            $num = mt_rand(0, $len);
            $randstr .= $str[$num];
        }
        return $randstr;
    }

        /**
	 * 发送http请求
	 * @param  string $url    http地址
	 * @param  array $params 提交参数
	 * @return string         http返回的结果
	 */
	public static function get($url, $params = null, $header = null, $u = null)
    {
        $ssl = substr($url, 0, 8) == "https://" ? TRUE : FALSE;
        $ch = curl_init();
        $opt = array(
            CURLOPT_URL => $url,
            CURLOPT_POST => TRUE,
            CURLOPT_POSTFIELDS => $params,
            CURLOPT_RETURNTRANSFER => TRUE,
            CURLOPT_TIMEOUT => 10,
            CURLOPT_HTTPHEADER => $header ? $header : [],
            CURLOPT_USERPWD => $u ? $u : '',
        );
        if ($ssl) {
            $opt[CURLOPT_SSL_VERIFYHOST] = 2;
            $opt[CURLOPT_SSL_VERIFYPEER] = false;
        }
        curl_setopt_array($ch, $opt);
        $data = curl_exec($ch);
        if ($data === false) {
            echo curl_error($ch);
            exit;
        }
        curl_close($ch);
        return json_decode($data, true);

    }


    public static function download($url, $path = 'images/'){
        if($url){
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
            $file = curl_exec($ch);
            curl_close($ch);
            $filename = pathinfo($url, PATHINFO_BASENAME);
            $resource = fopen($path . $filename, 'a');
            fwrite($resource, $file);
            fclose($resource);
            return '/'.$path . $filename;
        }else{
            return false;
        }
    }



    public static function http($url, $method, $postfields=null, $cookie=null, $timeout=3, $headers=array()) {
        $ci = curl_init();
        curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($ci, CURLOPT_USERAGENT, 'curl'); //避免index.php >> is_mobile_request() Notice: Undefined index: HTTP_USER_AGENT
        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 3);
        curl_setopt($ci, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ci, CURLOPT_ENCODING, "");

        curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, 2);

        curl_setopt($ci, CURLOPT_HEADER, false);

        if ($method == 'POST')
        {
            curl_setopt ( $ci, CURLOPT_POST, TRUE );
            if (! empty ( $postfields )) {
                curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
            }
        }

        if($cookie){
            curl_setopt ( $ci, CURLOPT_COOKIE, $cookie );
        }

        curl_setopt($ci, CURLOPT_URL, $url );
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers );
        curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE );

        $response = curl_exec($ci);

        if($response === false)
        {
            echo curl_error($ci);
            exit;
        }
        curl_close ($ci);

        return json_decode($response,true);
    }

}